<?php

class LoginModel {
	
	/** 
	 * Name of Session.
	 * @var String
	 */
	private static $ls = "my_LoginSession";
	
	/**
	 * @var String
	 */
	private static $lsLogin = "Login";
	
	/**
	 * @var String
	 */
	private static $lsUsername = "Username";
	
	/**
	 * @var String
	 */
	private static $lsUserAgent = "UserAgent";
	
	/**
	 * @var String
	 */
	private static $lsIP = "IP";
	
	/**
	 * @var String
	 */
	private static $lsTimeStamp = "TimeStamp";
	
	public function __construct() {
		if(!isset($_SESSION[self::$ls])){
			$this->setLoginSession();
		}
	}

	public function setLoginSessionLoggedIn($username) {
		$_SESSION[self::$ls][self::$lsLogin] = true;
		$_SESSION[self::$ls][self::$lsUsername] = $username;
	}

	public function setLoginSessionLoggedOut() {
		session_unset();
		session_regenerate_id(true);
	}
	
	/**
	 * Setup the session for login.
	 */
	private function setLoginSession() {
		$_SESSION[self::$ls] = array();
		$_SESSION[self::$ls][self::$lsUserAgent] = $_SERVER["HTTP_USER_AGENT"];
		$_SESSION[self::$ls][self::$lsIP] = $_SERVER["REMOTE_ADDR"];
	}
	
	/**
	 * Check if the session was hijacked.
	 * @return Boolean
	 */
	public function checkLoginSession() {
		return $_SESSION[self::$ls][self::$lsUserAgent] == $_SERVER["HTTP_USER_AGENT"] &&
			   $_SESSION[self::$ls][self::$lsIP] == $_SERVER["REMOTE_ADDR"];
	}
	
	/**
	 * @return Boolean
	 */
	public function userIsLoggedIn() {
		return isset($_SESSION[self::$ls][self::$lsLogin]);
	}
	
	/**
	 * @return string
	 */
	public function getUsername() {
		return $_SESSION[self::$ls][self::$lsUsername];
	}
}
